
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	null "gopkg.in/guregu/null.v3"
	"log"
	"time"
)

type Extensions struct {
	Name                 string          `db:"name" json:"name"`
	ShortName            null.String     `db:"short_name" json:"shortName"`
	Description          null.String     `db:"description" json:"description"`
	Version              string          `db:"version" json:"version"`
	InfoUrl              string          `db:"info_url" json:"infoUrl"`
	ScriptFile           string          `db:"script_file" json:"scriptFile"`
	Active               bool            `db:"active" json:"active"`
	AdditionalConfigJson null.String     `db:"additional_config_json" json:"additionalConfigJson"`
	CreatedAt            time.Time       `db:"created_at" json:"createdAt"`
	Links                ExtensionsLinks `json:"_links" db:-`
}

type ExtensionsLinks struct {
	Self                string              `db:"self" json:"_self"`
	ExtensionsTypesLink ExtensionsTypesLink `json:"extensions_types" db:-`
}

// @Title getExtensionsById
// @Description retrieves the extensions information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    Extensions
// @Resource /api/2.0
// @Router /api/2.0/extensions/{id} [get]
func getExtension(name string, db *sqlx.DB) (interface{}, error) {
	ret := []Extensions{}
	arg := Extensions{}
	arg.Name = name
	queryStr := "select *, concat('" + API_PATH + "extensions/', name) as self"
	queryStr += ", concat('" + API_PATH + "extensions_types/', type) as extensions_types_name_ref"
	queryStr += " from extensions WHERE name=:name"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getExtensionss
// @Description retrieves the extensions
// @Accept  application/json
// @Success 200 {array}    Extensions
// @Resource /api/2.0
// @Router /api/2.0/extensions [get]
func getExtensions(db *sqlx.DB) (interface{}, error) {
	ret := []Extensions{}
	queryStr := "select *, concat('" + API_PATH + "extensions/', name) as self"
	queryStr += ", concat('" + API_PATH + "extensions_types/', type) as extensions_types_name_ref"
	queryStr += " from extensions"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postExtensions
// @Description enter a new extensions
// @Accept  application/json
// @Param                 Body body     Extensions   true "Extensions object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/extensions [post]
func postExtension(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v Extensions
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO extensions("
	sqlString += "name"
	sqlString += ",short_name"
	sqlString += ",description"
	sqlString += ",version"
	sqlString += ",info_url"
	sqlString += ",script_file"
	sqlString += ",active"
	sqlString += ",additional_config_json"
	sqlString += ",type"
	sqlString += ",created_at"
	sqlString += ") VALUES ("
	sqlString += ":name"
	sqlString += ",:short_name"
	sqlString += ",:description"
	sqlString += ",:version"
	sqlString += ",:info_url"
	sqlString += ",:script_file"
	sqlString += ",:active"
	sqlString += ",:additional_config_json"
	sqlString += ",:type"
	sqlString += ",:created_at"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putExtensions
// @Description modify an existing extensionsentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     Extensions   true "Extensions object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/extensions/{id}  [put]
func putExtension(name string, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg Extensions
	err := json.Unmarshal(payload, &arg)
	arg.Name = name
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE extensions SET "
	sqlString += "name = :name"
	sqlString += ",short_name = :short_name"
	sqlString += ",description = :description"
	sqlString += ",version = :version"
	sqlString += ",info_url = :info_url"
	sqlString += ",script_file = :script_file"
	sqlString += ",active = :active"
	sqlString += ",additional_config_json = :additional_config_json"
	sqlString += ",type = :type"
	sqlString += ",created_at = :created_at"
	sqlString += " WHERE name=:name"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delExtensionsById
// @Description deletes extensions information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    Extensions
// @Resource /api/2.0
// @Router /api/2.0/extensions/{id} [delete]
func delExtension(name string, db *sqlx.DB) (interface{}, error) {
	arg := Extensions{}
	arg.Name = name
	result, err := db.NamedExec("DELETE FROM extensions WHERE name=:name", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
